package May21;
import java.util.*;
import java.util.stream.Stream;
import X20230510.Student;
public class OptionalStudents {
    public static void main(String[] args) {
        StudentsStream.stream()
                .limit(5)
                .filter(Optional::isPresent)
                .filter(o->o.get().getAGE()>18)
                .map(Optional::get)
                .forEach(System.out::println);
    }
    public static class StudentsStream extends Student {
        static List<Student> name = Arrays.asList(
                new Student("唐顺有","1",13),
                new Student("薛佛世","2",11),
                new Student("易江维","3",22),
                new Student("时党舒","4",19),
                new Student("周卓浩","5",33)

        );
        static Random random = new Random();
        public StudentsStream(String NAME, String STUDENT_ID, int AGE) {
            super(NAME, STUDENT_ID, AGE);
        }
        public static Student NewStu(){
            int num = random.nextInt(3);
            int index = random.nextInt(name.size());
            switch (num){
                case 0: return name.get(index);
                case 1: return name.get(index);
                case 2: return name.get(index);
                default: return null;
            }

        }

        public static Stream<Optional<Student>> stream(){
            return Stream.generate(StudentsStream::NewStu)
                    .map( student-> Optional.ofNullable(student));
        }
    }
}
